package cc.telecomdigital.tdstock.Tools;

import cc.telecomdigital.tdstock.R;

import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.Abstract_ActivityController;
import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;

//The activity be a unique activity.  That is only possible to perform one job
//Must have a group Host
public class ActivityGroupController extends Abstract_ActivityController
{
	private ActivityGroup	hostActivity;
	protected boolean		transitAnimation;
	protected int			animatedTime;
	// Forward to next Page GUI, Transition animation
	protected int			nextRInAnimation;
	protected int			nextROutAnimation;

	// Backward to previous Page, Transition animation
	protected int			backRInAnimation;
	protected int			backROutAnimation;
	
//	private final int defaultNextRInAnimation = R.anim.slide_in_right2;
//	private final int defaultNextROutAnimation = R.anim.slide_out_left2;
//
//	private final int defaultBackRInAnimation = R.anim.slide_in_left2;
//	private final int defaultBackROutAnimation = R.anim.slide_out_right2;	
	
	private final int defaultNextRInAnimation = R.anim.slide_in_right;
	private final int defaultNextROutAnimation = R.anim.slide_out_left;

	private final int defaultBackRInAnimation = R.anim.slide_in_left;
	private final int defaultBackROutAnimation = R.anim.slide_out_right;		
	private final int defaultAnimatedTime = 200;
	
	private int defaultIntentFlag;
	public ActivityGroupController(ActivityGroup iHost)
	{
		super();
		hostActivity = iHost;
		defaultIntentFlag  = (Intent.FLAG_ACTIVITY_NO_ANIMATION| Intent.FLAG_ACTIVITY_CLEAR_TOP);
		//defaultIntentFlag |= Intent.FLAG_ACTIVITY_NEW_TASK;
		//defaultIntentFlag |= Intent.FLAG_ACTIVITY_NO_USER_ACTION;		
		RestoreToDefaultAnimation();
	}
	
	/** Update to activity host for the object **/
	public void UpdateGroupHost(ActivityGroup iHost)
	{
		hostActivity = iHost;
	}
	
	/** Restore the animation to the defined values **/
	public void RestoreToDefaultAnimation()
	{
		transitAnimation = false;
		nextRInAnimation  = defaultNextRInAnimation;
		nextROutAnimation = defaultNextROutAnimation;

		backRInAnimation  = defaultBackRInAnimation;
		backROutAnimation = defaultBackROutAnimation;
		animatedTime = defaultAnimatedTime;
	}	
	
	/** HostActivity is the activity that perform the activity switching work **/
	public ActivityGroup GetHostActivity()
	{
		return hostActivity;
	}
	
	@Override
	protected String GetModuleIdentity()
	{
		return "ActivityGroupController";
	}
	
	// Once it is call destroy is not usable anymore
	@Override
	public void DestoryController()
	{
		if (hostActivity != null)
		{
			hostActivity.getLocalActivityManager().removeAllActivities();
			hostActivity = null;
		}
		super.DestoryController();
	}

	public void ResetController()
	{
		ActivityClearList();		
		if (hostActivity != null)
			hostActivity.getLocalActivityManager().removeAllActivities();
	}
	
	
	/** This would just go back to the previous list selection, the intent
	    source and destination will be modified **/
	@Override
	public boolean ActivityBackwardSwitch(Activity iCallActivity,
			Intent iIntentObj)
	{
		synchronized (listLock)
		{
			Class<? extends Activity> toActivityClass;

			// We are at the root, nothing we can change
			if (indexOfCurActivity < 0)
				return false;

			// Back to the Host
			if (indexOfCurActivity == 0)
			{
				indexOfCurActivity = -1;
				hostActivity.onBackPressed();
				return true;
			}

			try
			{
				activityClassList = activityClassList.subList(0,
													indexOfCurActivity);
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
				TDStockLog.d(LOG_TAG, "ActivityBackwardSwitch=>Exception: " + ex.toString());
			}
			
			// Get Activity and shorten the list
			toActivityClass = activityClassList.get(indexOfCurActivity - 1);
			ActivitySwitchPerform(iCallActivity, toActivityClass, iIntentObj,
					CHANGE_DIRECTION.BACK);


			indexOfCurActivity = activityClassList.size() - 1;
			return true;
		}
	}

	public boolean IsTransitWithAnimation()
	{
		return transitAnimation;
	}	
	
	/** Set whether the animation is required **/
	public void TransitWithAnimation(boolean iEnable)
	{
		transitAnimation = iEnable;
	}

	/** Customize the directional animation resource for changing to next **/
	public void SetForwardAnimation(int iPageInAnimate, int iPageOutAnimate)
	{
		nextRInAnimation = iPageInAnimate;
		nextROutAnimation = iPageOutAnimate;
	}

	/** Customize the directional animation resource for changing to back **/
	public void SetBackwardAnimation(int iPageInAnimate, int iPageOutAnimate)
	{
		backRInAnimation = iPageInAnimate;
		backROutAnimation = iPageOutAnimate;
	}

	/** Set the duration for the animation, half for out and half for in **/
	public void SetDurationAnimation(int iDuration)
	{
		animatedTime = iDuration;
	}

	/** Enforce a switching of Activity, however user must use with caution, this
	 * is designed when the List is being first invoke.  There will be no memory control
	 * and no animation
	 * @param iChangingActClass
	 */
	public void ActivityEnforceSwitch(Class<? extends Activity> iChangingActClass)
	{
		Intent intentObj;
		View   nextView;
			
		intentObj = new Intent();
		intentObj.setClass(hostActivity, iChangingActClass);
		if (defaultIntentFlag != 0)
			intentObj.addFlags(defaultIntentFlag);

		if (debugMode)
			TDStockLog.i(LOG_TAG, "ActivityEnforceSwitch to " + iChangingActClass.toString());
		
		String curId;
		
		curId = hostActivity.getLocalActivityManager().getCurrentId();
		//Delete any non - use activity in our window
		if (IsDestroyOnFinish())
			hostActivity.getLocalActivityManager().dispatchDestroy(true);
				
		// Host Activity is the activity manager, or the mother of Activity
		nextView = hostActivity.getLocalActivityManager()
				.startActivity(iChangingActClass.toString(), intentObj)
				.getDecorView();

		hostActivity.setContentView(nextView);
		
		if(curId != null && IsDestroyOnFinish() && (curId.equals(iChangingActClass.toString()) == false))
			hostActivity.getLocalActivityManager().destroyActivity(curId, true);		
	}
	
	@Override
	protected void ActivitySwitchPerform(Activity iCallActivity,
			Class<? extends Activity> iChangingActClass, Intent iIntentObj,
			CHANGE_DIRECTION iSwitchDir)
	{
		//Ignore the change if they are the same
		if (iCallActivity != null)
		{
			if (iCallActivity.getClass().equals(iChangingActClass))
				return;
		}
		
		iIntentObj.setClass(hostActivity, iChangingActClass);
		if (defaultIntentFlag != 0)
			iIntentObj.addFlags(defaultIntentFlag);
		
		//Clean up the mess
		final String stringId;
		
		stringId = hostActivity.getLocalActivityManager().getCurrentId();

		final View nextView;
		// Host Activity is the activity manager, or the mother of Activity
		nextView = hostActivity.getLocalActivityManager()
				.startActivity(iChangingActClass.toString(), iIntentObj)
				.getDecorView();
	
		if (transitAnimation)
		{
			Animation animatAction1;
			final Animation animatAction2;
			View curView;

			//Get the CurView
			curView = hostActivity.getWindow().getDecorView();
			if (iSwitchDir == CHANGE_DIRECTION.BACK)
			{
				animatAction1 = AnimationUtils.loadAnimation(hostActivity,
						backROutAnimation);
				animatAction2 = AnimationUtils.loadAnimation(hostActivity,
						backRInAnimation);
			}
			else
			{
				animatAction1 = AnimationUtils.loadAnimation(hostActivity,
						nextROutAnimation);
				animatAction2 = AnimationUtils.loadAnimation(hostActivity,
						nextRInAnimation);
			}

			animatAction1.setDuration(animatedTime >> 1);
			animatAction2.setDuration(animatedTime >> 1);
			animatAction1.setAnimationListener(new AnimationListener()
			{
				@Override
				public void onAnimationEnd(Animation animation)
				{
					nextView.startAnimation(animatAction2);
					hostActivity.setContentView(nextView);
					if (IsDestroyOnFinish() && (stringId != null))
						hostActivity.getLocalActivityManager().destroyActivity(stringId, true);					
				}

				@Override
				public void onAnimationRepeat(Animation animation)
				{
				}

				@Override
				public void onAnimationStart(Animation animation)
				{
				}

			});
			//curView.startAnimation(animatAction1);
			//This would enforce the animation, even the UI is not shown
			curView.startAnimation(animatAction1);
		}
		else
		{
			hostActivity.setContentView(nextView);
			if (IsDestroyOnFinish() && (stringId != null))
				hostActivity.getLocalActivityManager().destroyActivity(stringId, true);
		}
	}
}
